﻿using System.Collections.Generic;
using SharpDX;
using SharpDX.Toolkit.Graphics;

namespace BoatNGoat
{
    public class SpriteSheet
    {
        private readonly int _spriteCountX;
        private readonly int _spriteCountY;
        private readonly int _padding;
        private readonly Texture2D _texture;
        private readonly int _spriteWidth;
        private readonly int _spriteHeight;
        private readonly Vector2 _origin;
        private readonly int _twoPadding;

        public SpriteSheet(int spriteCountX, int spriteCountY, int padding, Texture2D texture)
        {
            _spriteCountX = spriteCountX;
            _spriteCountY = spriteCountY;
            _padding = padding;
            _twoPadding = _padding*2;
            int widthPadding = _twoPadding*spriteCountX;
            int heightPadding = _twoPadding*spriteCountY;
            _texture = texture;
            _spriteWidth = (texture.Width - widthPadding)/spriteCountX;
            _spriteHeight = (texture.Height - heightPadding)/spriteCountY;
            _origin = new Vector2(_spriteWidth/2, _spriteHeight/2);
        }

        public int SpriteHeight
        {
            get { return _spriteHeight; }

        }

        public int SpriteWidth
        {
            get { return _spriteWidth; }

        }

        public Texture2D Texture
        {
            get { return _texture; }
        }

        public int SpriteCountY
        {
            get { return _spriteCountY; }
        }

        public int SpriteCountX
        {
            get { return _spriteCountX; }
        }

        public Vector2 Origin
        {
            get { return _origin; }
        }

        public DrawingRectangle GetSourceRectangle(int spriteX, int spriteY)
        {
            return new DrawingRectangle(spriteX*(_spriteWidth + _twoPadding) + 1,
                                        spriteY*(_spriteHeight + _twoPadding) + 1, _spriteWidth, _spriteHeight);
        }
    }
}